package main

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func buildTree2(inorder []int, postorder []int) *TreeNode {
	if len(inorder) == 0 {
		return nil
	}
	rootVal := postorder[len(postorder)-1]
	root := &TreeNode{Val: rootVal}
	i := 0
	for inorder[i] != rootVal {
		i++
	}

	root.Left = buildTree2(inorder[0:i], postorder[:len(inorder[0:i])])
	root.Right = buildTree2(inorder[i+1:], postorder[len(inorder[0:i]):len(postorder)-1])
	return root
}
